package com.oim.core.common.box;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.oim.core.bean.Group;
import com.oim.core.bean.GroupMember;

/**
 * 
 * @author XiaHui
 * @date 2016年1月17日 下午1:49:49
 * @version 0.0.1
 */
public class GroupBox {

	private static Map<String, Group> groupMap = new ConcurrentHashMap<String, Group>();
	private static Map<String, List<GroupMember>> groupMemberListMap = new ConcurrentHashMap<String, List<GroupMember>>();

	public static void put(String id, Group group) {
		groupMap.put(id, group);
	}

	public static Group get(String id) {
		return groupMap.get(id);
	}

	public static void put(String groupId, List<GroupMember> list) {
		groupMemberListMap.put(groupId, list);
	}

	public static List<GroupMember> getGroupMemberList(String groupId) {
		return groupMemberListMap.get(groupId);
	}

	public static GroupMember getGroupMember(String groupId, String userId) {
		GroupMember gm = null;
		List<GroupMember> list = groupMemberListMap.get(groupId);
		if (null != list) {
			for (GroupMember g : list) {
				if (userId.equals(g.getUserId())) {
					gm = g;
					break;
				}
			}
		}
		return gm;
	}

	public static String getGroupPosition(String groupId, String userId) {
		String position = "3";
		List<GroupMember> list = groupMemberListMap.get(groupId);
		if (null != list) {
			for (GroupMember g : list) {
				if (userId.equals(g.getUserId())) {
					position = g.getPosition();
					break;
				}
			}
		}
		return position;
	}

	public static boolean isOwner(String groupId, String userId) {
		String position = getGroupPosition(groupId, userId);
		boolean mark = GroupMember.position_owner.equals(position);
		return mark;
	}

	public static boolean isNormal(String groupId, String userId) {
		String position = getGroupPosition(groupId, userId);
		boolean mark = !GroupMember.position_owner.equals(position) && !GroupMember.position_admin.equals(position);
		return mark;
	}

	public static boolean isAdmin(String groupId, String userId) {
		String position = getGroupPosition(groupId, userId);
		boolean mark = GroupMember.position_admin.equals(position);
		return mark;
	}
}
